package com.fly.toolbox;

import com.fly.toolbox.component.AppSettingsComponent;
import com.fly.toolbox.config.ApplicationProperties;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.options.Configurable;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class ApplicationSettingsConfigurable implements Configurable {

private AppSettingsComponent mySettingsComponent;

    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return "Toolbox Settings";
    }

    @Override
    public JComponent getPreferredFocusedComponent() {
        return mySettingsComponent.getPreferredFocusedComponent();
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        mySettingsComponent = new AppSettingsComponent();
        return mySettingsComponent.getPanel();
    }

    @Override
    public boolean isModified() {

        return true;
    }

    @Override
    public void apply() {
        PropertiesComponent.getInstance().setValue("user", mySettingsComponent.getUserText());
        PropertiesComponent.getInstance().setValue("versions", mySettingsComponent.getVersionsText());
        PropertiesComponent.getInstance().setValue("baseClassPath", mySettingsComponent.getBaseClassPathText());
        PropertiesComponent.getInstance().setValue("exportFilePath", mySettingsComponent.getExportFilePathText());
        if (mySettingsComponent.getClearHistoryStatus()) {
            PropertiesComponent.getInstance().setValue("history", null);
        }
        ApplicationProperties.getInstance().init();
    }

    @Override
    public void reset() {
        mySettingsComponent.setUserText(PropertiesComponent.getInstance().getValue("user"));
        mySettingsComponent.setVersionsText(PropertiesComponent.getInstance().getValue("versions"));
        mySettingsComponent.setBaseClassPathText(PropertiesComponent.getInstance().getValue("baseClassPath"));
        mySettingsComponent.setExportFilePathText(PropertiesComponent.getInstance().getValue("exportFilePath"));
        mySettingsComponent.setClearHistoryStatus(false);
    }

    @Override
    public void disposeUIResources() {
        mySettingsComponent = null;
    }
}
